package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.YaoQingBean;
import com.oscar.ehome.bean.setbean.FeedBackBean;
import com.oscar.ehome.bean.setbean.TokenBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.upload.OnUploadListener;
import com.oscar.ehome.utils.upload.UploadUtil;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.MyEditText;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.view.YaoQingView;
import com.yanzhenjie.album.Album;

import org.feezu.liuli.timeselector.TimeSelector;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: SunHaoZhao
 * date: On 2018/1/24
 */

public class YaoQingCardActivity extends BasesActivity<YaoQingView, PagePresenter> implements YaoQingView, OnUploadListener {

    PagePresenter pagePresenter;



    public List<String> ImageList = new ArrayList<>();//上传图片

    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.titlebar_img_right)
    ImageView titlebarImgRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.right_layout)
    RelativeLayout rightLayout;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.yaoqingpicture)
    ImageView yaoqingpicture;
    @BindView(R.id.weixinpicture)
    ImageView weixinpicture;
    @BindView(R.id.nameedit)
    EditText nameedit;
    @BindView(R.id.jianjieedit)
    MyEditText jianjieedit;
    @BindView(R.id.yaoqingriqi)
    TextView yaoqingriqi;
    @BindView(R.id.quesiton_tj)
    Button quesitonTj;

    String gerenzhao, weixinma;
    private volatile boolean isRunning = false;//判断线程池是否运行 标志位
    private UploadUtil mUploadUtil;

    private String type;

    private String uptoken = "";
    String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yaoqinghanshuju);
        ButterKnife.bind(this);
        setColor();
        initView();

    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(YaoQingCardActivity.this).getSystemConfig().getColor()));
        TintManager.setTint(quesitonTj, Color.parseColor(SystemConfigFactory.getInstance(this).getSystemConfig().getColor()));
    }

    private void initView() {
        CommonUtils.url_code = 1;
        titleImgBack.setVisibility(View.VISIBLE);
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText("填写邀请");
        token = SystemConfigFactory.getInstance(YaoQingCardActivity.this).getSystemConfig().getToken();
        pagePresenter.getyaoqingtoken();
        mUploadUtil = new UploadUtil();
        mUploadUtil.setOnUploadListener(this);
        //pagePresenter.getyaoqinginfo(token);

    }

    @OnClick({R.id.title_layout_left, R.id.quesiton_tj, R.id.yaoqingpicture, R.id.weixinpicture, R.id.yaoqingriqi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.weixinpicture:
                type = "2";
                Album.startAlbum(YaoQingCardActivity.this, 400, 1);
                break;
            case R.id.yaoqingriqi:

                TimeSelector timeSelector = new TimeSelector(this, new TimeSelector.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        String[] strs = time.split("-");
                        yaoqingriqi.setText(strs[0] + "年" + strs[1] + "月" + strs[2].substring(0, 2) + "日" + strs[2].substring(3, 8));
                    }
                }, "2014-1-1 00:00", "2030-12-30 00:00");
                timeSelector.show();
                break;
            case R.id.yaoqingpicture://返回
                type = "1";
                Album.startAlbum(YaoQingCardActivity.this, 400, 1);

                break;
            case R.id.title_layout_left://返回
                finish();
                break;
            case R.id.quesiton_tj://提交
                //提交反馈
                SubmitFeedBack();
                break;
        }
    }

    private void SubmitFeedBack() {
        CommonUtils.url_code = 0;
        //判断为空
        if (nameedit.getText().length() == 0) {
            Toast.makeText(YaoQingCardActivity.this, "请填写姓名", Toast.LENGTH_SHORT).show();    //弹出一个自动消失的提示框
            return;
        }
        if (jianjieedit.getText().length() == 0) {
            Toast.makeText(YaoQingCardActivity.this, "请填写个人简介", Toast.LENGTH_SHORT).show();    //弹出一个自动消失的提示框
            return;
        }
        if (yaoqingriqi.getText().equals("请选择邀请日期")) {
            Toast.makeText(YaoQingCardActivity.this, "请选择日期", Toast.LENGTH_SHORT).show();    //弹出一个自动消失的提示框
            return;
        }
        //获取手机品牌

        String name = nameedit.getText().toString();
        String jianjie = jianjieedit.getText().toString();
        String shijian = yaoqingriqi.getText().toString();

        if (gerenzhao != null && !gerenzhao.equals("")) {
            if (weixinma != null && !weixinma.equals("")) {
                showLoading();
                pagePresenter.yaoqingfankui(token, name, jianjie, shijian, gerenzhao, weixinma);
            } else {
                Toast.makeText(YaoQingCardActivity.this, "请上传个人微信二维码", Toast.LENGTH_SHORT).show();
            }

        } else {
            Toast.makeText(YaoQingCardActivity.this, "请上传个人形象照", Toast.LENGTH_SHORT).show();
        }


    }

    //   获取上传到七牛图片的token值
    @Override
    public void getyaoqingtoken(TokenBean tokenBean) {
        uptoken = tokenBean.getUptoken();
    }

    @Override
    public void getyaoqinginfo(YaoQingBean result) {
        if (result != null && !result.equals("")) {
            Intent in = getIntent();
            if (in.getStringExtra("bianji") != null && in.getStringExtra("bianji").equals("bianji")) {
                //复制
                nameedit.setText(result.getName());
                jianjieedit.setText(result.getIntroduction());
                yaoqingriqi.setText(result.getTime());
                gerenzhao = result.getImage();
                weixinma = result.getQrCode();
                GildeUtils.loadUrlImages(C.qiniu + result.getImage(), yaoqingpicture, this);
                GildeUtils.loadUrlImages(C.qiniu + result.getQrCode(), weixinpicture, this);
            } else {
                pagePresenter.yaoqingfankui(token, result.getName(), result.getIntroduction(), result.getTime(), result.getImage(), result.getQrCode());
            }
        }
    }

    @Override
    public void yaoqingfankui(FeedBackBean feedBackBean) {
        dismissLoading();

        Intent in = new Intent();
        SystemConfigFactory.getInstance(YaoQingCardActivity.this).getSystemConfig().setyaoqing(feedBackBean.getObject());
        in.putExtra("contentsrc", feedBackBean.getObject());
        in.putExtra("type", "yaoqing");
        in.putExtra("name", "邀请函");
        in.putExtra("touxiang", gerenzhao);
        in.setClass(YaoQingCardActivity.this, BannerWebActivity.class);
        startActivity(in);
        finish();
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(YaoQingCardActivity.this, getString(R.string.erry));

    }

    @Override
    public void erry(String result) {
        dismissLoading();
        if (!result.equals("tiaoguo")) {
            Toast.makeText(YaoQingCardActivity.this, result, Toast.LENGTH_SHORT).show();
        }
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(YaoQingCardActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(YaoQingCardActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }


    }


    @Override
    public void onAllSuccess() {
        isRunning = false;

        Log.d("mwh", "文件" + "全部上传成功");
    }

    @Override
    public void onAllFailed() {
        isRunning = false;

        Log.d("mwh", "文件" + "全部失败");
    }

    @Override
    public void onThreadProgressChange(int position, int percent) {
        Log.d("mwh", "文件" + position + "上传" + percent + "%");

    }

    @Override
    public void onThreadFinish(int position, String keys) {
        if (type.equals("1")) {
            gerenzhao = keys;
        } else if (type.equals("2")) {
            weixinma = keys;
        }

    }

    @Override
    public void onThreadInterrupted(int position) {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 400) {
            if (resultCode == RESULT_OK) { // 判断是否成功。
                // 拿到用户选择的图片路径List：
                ImageList.clear();
                ImageList = Album.parseResult(data);

                if ("".equals(uptoken)) {
                    pagePresenter.getyaoqingtoken();
                } else {
                    if (ImageList.size() > 0) {
                        startUpload();
                    }
                }
                if (type.equals("1")) {
                    GildeUtils.loadUrlImages(ImageList.get(0), yaoqingpicture, this);
                } else if (type.equals("2")) {
                    GildeUtils.loadUrlImages(ImageList.get(0), weixinpicture, this);
                }

            }
        }
    }

    private void startUpload() {//模拟上传文件
        isRunning = true;
        mUploadUtil.submitAll(ImageList, uptoken);
    }


}
